<?php
declare (strict_types = 1);

namespace app\controller;

use app\model\T_PM_MO_BASE;
use app\model\T_PM_MO_ONLINE;
use app\model\T_WIP_ERROR;
use app\model\YdayModoc;
use Cassandra\Date;
use think\Request;

class Docs
{
    /**
     * 昨日制令单信息同步接口
     */
    public function DOCs()
    {
        //更改最后更新时间不为23:59:01秒的制令单状态
        if(date('Y-m-d H:i:s')>=date('Y-m-d 23:59:01')){
            $dataInfo=(new YdayModoc())->where(
                [
                    ['create_time','>=',date('Y-m-d 00:00:00')],
                    ['create_time','<=',date('Y-m-d 23:59:59')],
                    ['update_time','<=',date('Y-m-d 23:59:01')]
                ]
            )->select()->toArray();
            foreach ($dataInfo as $v){
                //获取最终状态
                $status=(new T_PM_MO_BASE())->where('MO_DOC','=',$v['MO_DOC'])->value('STATUS');
                //更新操作
                (new YdayModoc())->where([['MO_DOC','=',$v['MO_DOC']],['create_time','>',date('Y-m-d 00:00:00')],['create_time','<',date('Y-m-d 23:59:59')]])->update(['Is_finish'=>$status]);
            }
        }
        //获取制令单在制信息
        //SMT
        $fields = 'a.MO_DOC,b.ITEM_SPEC,a.PLAN_QTY,c.OUTPUT_QTY,c.STATUS';
        $res1 = (new T_PM_MO_ONLINE())->alias('a')
            ->leftJoin('T_CO_ITEM b', 'a.MODEL_CODE=b.ITEM_CODE')
            ->leftJoin('T_PM_MO_BASE c', 'a.MO_DOC=c.MO_DOC')
            ->field($fields)
            ->where('a.AREA_SN', 'like', '%SMT%')
            ->where('a.AREA_SN', '<>', 'SMT1-7')
            ->select()->toarray();

        foreach ($res1 as $v) {
            //计算不良率，不良数量/产出数量
            //产出数量不能为空
            if (intval($v['OUTPUT_QTY']) != 0) {
                //获取不良品数量
                $info2=(new T_WIP_ERROR())->where('MO_NUMBER',$v['MO_DOC'])->where('CREATE_BY','=','AOI')->field('NG_QTY')->find();
                //不良率
                $NReached =intval($info2['NG_QTY'])/intval($v['OUTPUT_QTY']);
                $NReached=(substr((string)$NReached,0,5));
                //达成率=生产数量/计划数量
                $Reached = intval($v['OUTPUT_QTY'])/intval($v['PLAN_QTY'])*100;
                $Reached=(substr((string)$Reached,0,5));
            } else {
                $Reached = 0;
                $NReached = 0;
            }
            $data1 = [
                'MO_DOC' => $v['MO_DOC'],
                'ITEM_SPEC' => $v['ITEM_SPEC'],
                'PLAN_QTY' => intval($v['PLAN_QTY']),
                'OUTPUT_QTY' => intval($v['OUTPUT_QTY']),
                'type' => 1,//smt
                'Is_finish' => $v['STATUS'],
                'Reached' => $Reached,
                'NG' => $NReached,
                'create_time' => date('Y-m-d H:i:s')
            ];
            //重复的话做更新处理
            //获取记录数据制令单生产
            $info1=(new YdayModoc())->where([['MO_DOC','=',$v['MO_DOC']],['type','=',1],['create_time','>',date('Y-m-d 00:00:00')],['create_time','<',date('Y-m-d 23:59:59')]])->find();
            if($info1){
                //更新
                $data1s=[
                    'MO_DOC' => $v['MO_DOC'],
                    'ITEM_SPEC' => $v['ITEM_SPEC'],
                    'PLAN_QTY' => intval($v['PLAN_QTY']),
                    'OUTPUT_QTY' => intval($v['OUTPUT_QTY']),
                    'type' => 1,//smt
                    'Is_finish' => $v['STATUS'],
                    'Reached' => $Reached,
                    'NG' => $NReached,
                    'update_time'=>date('Y-m-d H:i:s')
                ];
                (new YdayModoc())->where([['MO_DOC','=',$v['MO_DOC']],['type','=',1],['create_time','>',date('Y-m-d 00:00:00')],['create_time','<',date('Y-m-d 23:59:59')]])->update($data1s);
            }else{
                (new YdayModoc())->insert($data1);
            }
        }
        //DIP
        $fields = 'a.MO_DOC,b.ITEM_SPEC,a.PLAN_QTY,c.OUTPUT_QTY,c.SCRAP_QTY,c.STATUS';
        $res2 = (new T_PM_MO_ONLINE())->alias('a')
            ->leftJoin('T_CO_ITEM b', 'a.MODEL_CODE=b.ITEM_CODE')
            ->leftJoin('T_PM_MO_BASE c', 'a.MO_DOC=c.MO_DOC')
            ->field($fields)
            ->where('a.AREA_SN', 'like', '%DIP%')
            ->select()->toarray();
        foreach ($res2 as $v) {
            //计算报废率，报废数量/产出数量
            //产出数量不能为空
            if (intval($v['OUTPUT_QTY']) != 0) {
                //报废率
                $NReached = intval($v['SCRAP_QTY']) / intval($v['OUTPUT_QTY']);
                //达成率=生产数量/计划数量
                $Reached = intval($v['OUTPUT_QTY'])/intval($v['PLAN_QTY'])*100;
                $Reached=(substr((string)$Reached,0,5));
            } else {
                $Reached = 0;
                $NReached = 0;
            }
            $data2 = [
                'MO_DOC' => $v['MO_DOC'],
                'ITEM_SPEC' => $v['ITEM_SPEC'],
                'PLAN_QTY' => intval($v['PLAN_QTY']),
                'OUTPUT_QTY' => intval($v['OUTPUT_QTY']),
                'type' => 2,//dip
                'Is_finish' => $v['STATUS'],
                'Reached' => $Reached,
                'NG' => $NReached,
                'create_time' => date('Y-m-d H:i:s')
            ];
            //重复的话做更新处理
            //获取记录数据制令单生产
            $info1=(new YdayModoc())->where([['MO_DOC','=',$v['MO_DOC']],['type','=',2],['create_time','>',date('Y-m-d 00:00:00')],['create_time','<',date('Y-m-d 23:59:59')]])->find();
            if($info1){
                //更新
                $data2s=[
                    'MO_DOC' => $v['MO_DOC'],
                    'ITEM_SPEC' => $v['ITEM_SPEC'],
                    'PLAN_QTY' => intval($v['PLAN_QTY']),
                    'OUTPUT_QTY' => intval($v['OUTPUT_QTY']),
                    'type' => 2,//dip
                    'Is_finish' => $v['STATUS'],
                    'Reached' => $Reached,
                    'NG' => $NReached,
                    'update_time'=>date('Y-m-d H:i:s')
                ];
                (new YdayModoc())->where([['MO_DOC','=',$v['MO_DOC']],['type','=',2],['create_time','>',date('Y-m-d 00:00:00')],['create_time','<',date('Y-m-d 23:59:59')]])->update($data2s);
            }else{
                (new YdayModoc())->insert($data2);
            }
        }
        //总装
        $fields = 'a.MO_DOC,b.ITEM_SPEC,a.PLAN_QTY,c.OUTPUT_QTY,c.SCRAP_QTY,c.STATUS';
        $res3 = (new T_PM_MO_ONLINE())->alias('a')
            ->leftJoin('T_CO_ITEM b', 'a.MODEL_CODE=b.ITEM_CODE')
            ->leftJoin('T_PM_MO_BASE c', 'a.MO_DOC=c.MO_DOC')
            ->field($fields)
            ->where('a.AREA_SN', 'like', '%AST/PCK%')
            ->select()->toarray();
        foreach ($res3 as $v) {

            //计算报废率，报废数量/产出数量
            //产出数量不能为空
            if (intval($v['OUTPUT_QTY']) != 0) {
                //报废率
                $NReached = intval($v['SCRAP_QTY']) / intval($v['OUTPUT_QTY']);
                //达成率=生产数量/计划数量
                $Reached = intval($v['OUTPUT_QTY'])/intval($v['PLAN_QTY'])*100;
                $Reached=(substr((string)$Reached,0,5));
            } else {
                $Reached = 0;
                $NReached = 0;
            }
            $data3 = [
                'MO_DOC' => $v['MO_DOC'],
                'ITEM_SPEC' => $v['ITEM_SPEC'],
                'PLAN_QTY' => intval($v['PLAN_QTY']),
                'OUTPUT_QTY' => intval($v['OUTPUT_QTY']),
                'type' => 3,//总装
                'Is_finish' => $v['STATUS'],
                'Reached' => $Reached,
                'NG' => $NReached,
                'create_time' => date('Y-m-d H:i:s')
            ];
            //重复的话做更新处理
            //获取记录数据制令单生产
            $info1=(new YdayModoc())->where([['MO_DOC','=',$v['MO_DOC']],['type','=',3],['create_time','>',date('Y-m-d 00:00:00')],['create_time','<',date('Y-m-d 23:59:59')]])->find();
            if($info1){
                //更新
                $data3s=[
                    'MO_DOC' => $v['MO_DOC'],
                    'ITEM_SPEC' => $v['ITEM_SPEC'],
                    'PLAN_QTY' => intval($v['PLAN_QTY']),
                    'OUTPUT_QTY' => intval($v['OUTPUT_QTY']),
                    'type' => 3,//总装
                    'Is_finish' => $v['STATUS'],
                    'Reached' => $Reached,
                    'NG' => $NReached,
                    'update_time'=>date('Y-m-d H:i:s')
                ];
                (new YdayModoc())->where([['MO_DOC','=',$v['MO_DOC']],['type','=',3],['create_time','>',date('Y-m-d 00:00:00')],['create_time','<',date('Y-m-d 23:59:59')]])->update($data3s);
            }else{
                (new YdayModoc())->insert($data3);
            }
        }
    }
}
